package com.longkey.workflow.client.selector
{
	import flash.events.Event;
	
	import spark.components.CheckBox;
	import spark.components.gridClasses.GridItemRenderer;
	import spark.layouts.VerticalLayout;
	
	public class CheckBoxColumnItemRender extends GridItemRenderer
	{
		private var checkItem:CheckBox;
		private var changeHandleFunc:Function;
		override public function prepare(hasBeenRecycled:Boolean):void
		{
			super.prepare(hasBeenRecycled);
			var val:Number = 0;
			if(data){
				val = parseInt(data[this.column.dataField]);
			}
			if(val)
				this.checkItem.selected = true;
			else
				this.checkItem.selected = false;
			this.checkItem.addEventListener(Event.CHANGE, checkBoxChangeHandle);
		}
		
		private function checkBoxChangeHandle(event:Event):void{
			if(this.checkItem.selected){
				data[this.column.dataField] = "1";
			}
			else{
				data[this.column.dataField] = "0";
			}
			if(this.changeHandleFunc != null){
				this.changeHandleFunc(this.checkItem.selected, this.rowIndex, this.data);
			}
		}
		
		public function set changeHandle(func:Function):void{
			this.changeHandleFunc = func;
		}
		
		
		public function CheckBoxColumnItemRender(){
			var vl:VerticalLayout = new VerticalLayout();
			vl.horizontalAlign = "center";
			vl.verticalAlign = "middle";
			this.layout = vl;
			this.checkItem = new CheckBox();
			this.addElement(this.checkItem);
		}
	}
}